<template>
  <div>
    <!--面包屑-->
    <breadcrumb></breadcrumb>
    <!-- 卡片-->
    <el-card class="box-card">
      <div class="header">
        <el-page-header :content="this.name1+'与'+this.name2+'的对话'" @back="getBack()"></el-page-header>
      </div>
      <div class="msg_container" ref="msgContent">
        <div
          :class="{'rightmsg_content':item.LeftOrRight,'leftmsg_content':!item.LeftOrRight}"
          v-for="(item,index) in msg"
          :key="index"
        >
          <el-avatar size="medium" :src="imgUrl1" v-show="!item.LeftOrRight"></el-avatar>
          <div>
            <span class="text">{{item.content}}</span>
            <div class="date">{{item.date}}</div>
          </div>
          <el-avatar size="medium" :src="imgUrl2" v-show="item.LeftOrRight"></el-avatar>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import breadcrumb from '../breadcrumb.vue'
export default {
  data() {
    return {
      msg: [],
      name1: '',
      name2: '',
      imgUrl1: '', // 他的头像
      imgUrl2: '' // 我的头像
    }
  },
  methods: {
    getBack() {
      this.$router.push('/suphome/allMsg')
    },
    // 获取消息
    async getContent() {
      const getRes = await this.$http.post('/getContent', {
        option: 'admin',
        id: this.$route.query.id
      })
      if (getRes.meta.status !== 200) {
        return this.$Message.error('error')
      }
      // this.$Message.success('ok')
      return getRes.data
    }
  },
  mounted() {
    this.getContent().then(async data => {
      var name1 = data.name[0]
      var name2 = data.name[1]
      await data.msg.forEach(item => {
        item.LeftOrRight = item.name === name1 ? false : true
      })
      // 获取头像
      this.comm.getuserImg(name1, this).then(data => {
        this.imgUrl1 = data
      })
      this.comm.getuserImg(name2, this).then(data => {
        this.imgUrl2 = data
      })
      this.name1 = name1
      this.name2 = name2
      this.msg = data.msg
    })
  },
  components: {
    breadcrumb
  }
}
</script>

<style lang="less" scoped>
.header {
  display: flex;
}
.el-breadcrumb {
  margin-bottom: 10px;
}
.msg_container {
  overflow: auto;
  overflow-x: hidden;
  width: 100%;
  height: 330px;
  .leftmsg_content {
    height: 50px;
    display: flex;
    align-items: center;
    padding: 15px 0 0 20px;
    > div {
      margin-left: 5px;
      height: 100%;
      .text {
        display: inline-block;
        max-width: 100px;
        margin-top: 4px;
        padding: 5px;
        font-size: 13px;
        color: #fff;
        border-radius: 10px;
        background: #007ceb;
      }
      .date {
        margin-top: 5px;
        font-size: 11px;
        color: #999;
      }
    }
  }
  .rightmsg_content {
    height: 50px;
    display: flex;
    justify-content: center;
    padding: 15px 20px 0 0;
    > div {
      display: flex;
      flex-direction: column;
      height: 100%;
      margin-left: auto;
      .text {
        margin-left: auto;
        margin-right: 5px;
        display: inline-block;
        max-width: 100px;
        margin-top: 4px;
        padding: 5px;
        font-size: 13px;
        color: #fff;
        border-radius: 10px;
        background: #007ceb;
      }
      .date {
        margin-top: 5px;
        font-size: 11px;
        color: #999;
      }
    }
  }
}
</style>
